#!/bin/bash
cd ${0%/*} || exit 1;

usage ()
{
	echo "Usage: $0 <outputPath>";
	exit 1;
}

generateInstallScript ()
{
	# pack
	if [ ! -d $PACKFOLDER ]; then
		echo "Can not find pack folder!";
		exit 1;
	fi
	cd $PACKFOLDER;
	# zip -r ../pack.zip *;
	tar -zcvf ../pack.tar.gz *;
	if [ $? -eq 0 ]; then
		cd ..
		rm -rf $PACKFOLDER;
	else
		echo "Failed to pack!";
		exit 1;
	fi
	NEEDGLIBCVERSION=$(getconf GNU_LIBC_VERSION | tr -d "glibc ") 2>&1 > /dev/null;
	# install.sh
	echo "#!/bin/bash" > install.sh;
	echo "cd \${0%/*} || exit 1;" >> install.sh;
	echo "function version_lt() { test \"\$(echo \"\$@\" | tr \" \" \"\n\" | sort -rV | head -n 1)\" != \"\$1\"; }" >> install.sh;
	echo "NEEDGLIBCVERSION=$NEEDGLIBCVERSION" >> install.sh;
	echo "GLIBCVERSION=\$(getconf GNU_LIBC_VERSION | tr -d \"glibc \")" >> install.sh;
	echo "if version_lt \$GLIBCVERSION \$NEEDGLIBCVERSION; then" >> install.sh;
	echo -e "\techo \"Need glibc: \$NEEDGLIBCVERSION, Now is \$GLIBCVERSION!\";" >> install.sh;
	echo -e "\texit 1;" >> install.sh;
	echo "fi" >> install.sh;
	echo "sed -n -e '1,/^exit 0;\$/!p' \$0 > pack.tar.gz 2>/dev/null;" >> install.sh;
	echo "if [ -f pack.tar.gz ]; then" >> install.sh;
	echo -e "\tmkdir FastCAE;" >> install.sh;
	# unzip -x pack.zip -d FastCAE
	echo -e "\ttar -zxvf pack.tar.gz -C FastCAE;" >> install.sh;
	echo -e "\tif [ \$? -eq 0 ]; then" >> install.sh;
	echo -e "\t\trm pack.tar.gz;" >> install.sh;
	echo -e "\tfi" >> install.sh;
	echo "else" >> install.sh;
	echo -e "\techo 'Failed to unpack!';" >> install.sh;
	echo -e "\texit 1;" >> install.sh;
	echo "fi" >> install.sh;
	echo "echo 'Install Successfully!';" >> install.sh;
	echo "exit 0;" >> install.sh;

	if [ -f install.sh ] && [ -f pack.tar.gz ]; then
		cat install.sh pack.tar.gz > install.run;
		if [ $? -eq 0 ]; then
			chmod +x install.run;
			rm install.sh;
			rm pack.tar.gz;
			echo "Success to generate install.run!";
		else
			echo "Failed to generate install.run!"; 
		fi
	fi
}


collectDependentLibraries ()
{
	if [ $# -ne 1 ]; then
		echo "Invalid params!"
		return 1;
	fi

	if [ ! -f $1 ]; then
		echo "$1 is not a share object!";
		return 1;
	fi

	LIBARRAY=($(ldd $1 | grep -o "/.*" | grep -o "/.*/[^[:space:]]*"));
	BINDIR="$PWD/$PACKFOLDER/bin";
	if [ ! -d $BINDIR ];then
		mkdir -p $BINDIR;
	fi
	
	for LIBFILE in ${LIBARRAY[@]}
	do
		if [ -f $BINDIR/$(basename $LIBFILE) ]; then
			echo "$BINDIR/$(basename $LIBFILE) ... Exist!";
			cp "$LIBFILE" $BINDIR/;
		else
			echo "$BINDIR/$(basename $LIBFILE) ... Not Exist!";
			echo "copy $LIBFILE ...";
			cp -rfL "$LIBFILE" $BINDIR/;	
		fi
	done	
	return 0;
}

copyFile ()
{
	BINDIR="$PWD/$PACKFOLDER/bin";
	if [ ! -d $BINDIR ];then
		mkdir -p $BINDIR;
	fi
	# copy bin/*
	cp -rfL bin/* $BINDIR/;
	collectDependentLibraries $BINDIR/FastCAE
	if [ $? -ne 0 ]; then
		echo "Failed to collect dependent libraries for FastCAE: $BINDIR/FastCAE!";
		exit 1;
	fi
	for LIBFILE in $(ls $BINDIR/*so*)
	do
		collectDependentLibraries $LIBFILE
		if [ $? -ne 0 ]; then
			echo "Failed to collect dependent libraries for shared object: $LIBFILE!";
		exit 1;
	fi
	done

	QTPATH="/opt/Qt5.14.2/5.14.2/gcc_64/";
	QXCBPATH="/opt/Qt5.14.2/5.14.2/gcc_64/plugins/platforms/libqxcb.so";
	while [ ! -f $QXCBPATH ] 
	do
		read -p "Input path of qt5 which is used for compile FastCAE[ex:/opt/Qt5.14.2/5.14.2/gcc_64/]:" QTPATH
		if [[ ${QTPATH: -1} != "/" ]]; then
			 QTPATH=$QTPATH"/";
		fi
		QXCBPATH=$QTPATH"plugins/platforms/libqxcb.so";
	done

	collectDependentLibraries $QXCBPATH;
	if [ $? -ne 0 ]; then
		echo "Failed to collect dependent libraries for shared object: $QXCBPATH!";
		exit 1;
	fi
	
	PLATFORM=$QTPATH"plugins/platforms";
	XCBPATH=$QTPATH"plugins/xcbglintegrations";

	if [ -d $PLATFORM ] && [ ! -d $BINDIR/platforms ]; then
		cp -RL $PLATFORM $BINDIR/platforms;
		if [ -d $XCBPATH ] && [ ! -d $BINDIR/xcbglintegrations ]; then
			cp -RL $XCBPATH $BINDIR/xcbglintegrations;
		fi
	else
		echo "Can not find platforms!";
		exit 1;
	fi
	
	LIBARRAY=("ld-linux-x86-64.so.2" "libanl.so" "libanl.so.1" "libBrokenLocale.so" "libBrokenLocale.so.1" "libc_malloc_debug.so" "libc_malloc_debug.so.0" "libcrypt.so" "libcrypt.so.1" "libc.so" "libc.so.6" "libdl.so.2" "libmemusage.so" "libm.so" "libm.so.6" "libmvec.so" "libmvec.so.1" "libnsl.so.1" "libnss_compat.so" "libnss_compat.so.2" "libnss_db.so" "libnss_db.so.2" "libnss_dns.so.2" "libnss_files.so.2" "libnss_hesiod.so" "libnss_hesiod.so.2" "libpcprofile.so" "libpthread.so.0" "libresolv.so" "libresolv.so.2" "librt.so.1" "libthread_db.so" "libthread_db.so.1" "libutil.so.1")
	for LIBFILE in ${LIBARRAY[@]}
	do
		if [ -f $BINDIR/$LIBFILE ]; then
			rm $BINDIR/$LIBFILE;
			echo "rm -f $BINDIR/$LIBFILE ..... Success.";
		else
			echo "$BINDIR/$(basename $LIBFILE) ..... Not Found.";
		fi
	done
	

	echo "#!/bin/bash" > $BINDIR/start.sh;
	echo "cd \${0%/*} || exit 1;" >> $BINDIR/start.sh;
	echo "export LD_LIBRARY_PATH=\$PWD:\$LD_LIBRARY_PATH;" >> $BINDIR/start.sh;
	echo "export PYTHONHOME=\$PWD/../python37:\$PYTHONHOME;" >> $BINDIR/start.sh;
	echo "export PYTHONPATH=\$PWD/../python37/lib/python3.7/:\$PYTHONPATH;" >> $BINDIR/start.sh;
	echo '$PWD/FastCAE' >> $BINDIR/start.sh;
	chmod +x $BINDIR/start.sh;

	# copy ConfigFiles
	if [ -d ConfigFiles ]; then
		cp -rfL ConfigFiles $BINDIR/../;
	fi

	# copy examples
	if [ -d examples ]; then
		cp -rfL examples $BINDIR/../;
	fi

	# copy python37
	cp -rfL python37 $BINDIR/../;

	# copy temp
	if [ -d temp ]; then
		cp -rfL temp $BINDIR/../;
	fi

	# $BINDIR/start.sh
}

PACKFOLDER="PackFolder";

if [ $# -eq 0 ];then
	if [ -d bin ] && [ -d python37 ] && [ -f bin/FastCAE ]; then
		copyFile;
		generateInstallScript;
	else
		usage;
	fi
elif [ $# -eq 1 ];then
	if [ -f $1 ]; then
		cd $1;
	else
		usage;
	fi
	if [ -d bin ] && [ -d python37 ] && [ -f bin/FastCAE ]; then
		copyFile;
		generateInstallScript;
	else
		echo "$1 is not output folder of FastCAE!";
	fi
else
	usage;
fi




